﻿using Models.Common;
using Newtonsoft.Json;
using RabbitMQ.Client;
using System;
using System.Diagnostics;
using System.IO;
using System.Net.Http;
using System.Threading;
using Utils_Base;

namespace GateWayCommonService.GateWay
{
    public static class ServiceMQ_SystemCommand
    {

        public static void systemProcessStart(MQReqModel<MQSystemCommand> mQReqModel)
        {
            try
            {

                var process = new Process
                {
                    StartInfo = new ProcessStartInfo(mQReqModel.data.fileName, "")//"/bin/bash"
                };
                process.StartInfo.RedirectStandardInput = true;
                process.StartInfo.RedirectStandardOutput = true;
                process.StartInfo.UseShellExecute = false;
                process.Start();
                process.StandardInput.WriteLine(mQReqModel.data.command);
                process.StandardInput.Close();
                var cpuInfo = process.StandardOutput.ReadToEnd();

                var lines = cpuInfo.Split('\n');
                foreach (var item in lines)
                {
                    Console.WriteLine(DateTime.Now.ToString() + "行记录：" + item);
                }


                //MQReqModel<MQSystemCommand> mQReqModel = new MQReqModel<MQSystemCommand>();
                //更新状态       
                //mQReqModel.respEvent = MQEventEnum.Device_Command_Return;// "miniServerInfo_doCommand_status";
                mQReqModel.respCode = "2";
                mQReqModel.respMsg = cpuInfo;
                //mQReqModel.gateWayId = UtilsMachineInfo.getMachineId();
                //mQReqModel.data = mQSystemCommand;
                new ServiceRabbitMQ_DeviceHelper().sendMsg(JsonConvert.SerializeObject(mQReqModel));

            }
            catch (Exception ex)
            {
                //MQReqModel<MQSystemCommand> mQReqModel = new MQReqModel<MQSystemCommand>();
                //更新状态       
                //mQReqModel.respEvent = MQEventEnum.Device_Command_Return;// "miniServerInfo_doCommand_status";
                mQReqModel.respCode = "3";
                mQReqModel.respMsg = "system error";
                //mQReqModel.gateWayId = UtilsMachineInfo.getMachineId();
                //mQReqModel.data = mQSystemCommand;
                new ServiceRabbitMQ_DeviceHelper().sendMsg(JsonConvert.SerializeObject(mQReqModel));

                Console.WriteLine(ex.ToString());
            }

        }


    }
}
